﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Algorithms.Tools;

/*
 * TH07 Groep 10
 * ----------------------------------------------------------------------------------
 * Versie   Door        Commentaar
 * 0.1      Mike        Opzet en programmeer werk
 * 0.2      Luca        Optimalisatie code
 *                      Hernoemen variabelen en functies
 *                      overbodige code verwijderd 
 *                      crop image naar eigen tool classe verplaatst.
 *               
 */


namespace THO7AlgoritmTimerApplication
{
    class Character
    {

        #region private variables
        private readonly int VECTOR_SIZE = 5;
        private readonly int ADDITIONAL_PROPERTIES = 4;

        /// <summary>
        /// Byte Array containing the properties for a image
        /// 
        /// 0-25: saving avarage gray values per segment for an image.
        /// </summary>
        private byte[] properties;

        #endregion

        #region public setters and getters
        public byte[] Properties
        {
            get { return properties; }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public Character()
        {
            properties = new byte[VECTOR_SIZE * VECTOR_SIZE + ADDITIONAL_PROPERTIES];
        }
        #endregion

        #region public functions
        /// <summary>
        /// Calculate image properties
        /// </summary>
        /// <param name="img"></param>
        public void CalculateFromImage(VisionImage img)
        {
            img.Lock();

            int height = img.Height;
            int halfHeight = height / 2;
            int width = img.Width;
            int halfWidth = width / 2;
            int offset = img.Data.Stride - (width * 3);

            int arrayCounter = 0;

            int vectorWidth = img.Width / VECTOR_SIZE;
            int vectorHeight = img.Height / VECTOR_SIZE;
            int vectorPixels = vectorWidth * vectorHeight;

            unsafe
            {
                byte* charPointer = (byte*)img.Data.Scan0;

                int horizontalTimes = width / VECTOR_SIZE;
                int verticalTImes = height / VECTOR_SIZE;

                for (int vert = 0; vert < VECTOR_SIZE; vert++)
                {
                    for (int hor = 0; hor < VECTOR_SIZE; hor++)
                    {
                        byte* vectorPointer = charPointer;
                        int avgVectorGray = 0;
                        int lastPixel = 0;

                        for (int vectorY = 0; vectorY < vectorHeight; vectorY++)
                        {
                            for (int vectorX = 0; vectorX < vectorWidth; vectorX++)
                            {
                                lastPixel = (int)(0.1140 * vectorPointer[0] + 0.5870 * vectorPointer[1] + 0.2989 * vectorPointer[2]);
                                avgVectorGray += lastPixel;
                                vectorPointer++;
                            }
                            vectorPointer += ((width * 3) + offset - (vectorWidth * 3));
                        }
                        properties[arrayCounter++] = (byte)(avgVectorGray / vectorPixels);
                        charPointer += vectorWidth;
                    }
                    charPointer += ((vectorWidth * 3) + (vectorHeight * ((width * 3) + offset) + offset));
                }
            }
            img.Unlock();
        }
        #endregion

        
    }
}
